package cn.ggb.educenter.controller;


import cn.ggb.commonutils.JwtUtils;
import cn.ggb.commonutils.OrderVo.UcenterMemberVo;
import cn.ggb.commonutils.R;
import cn.ggb.educenter.entity.RegisterVo;
import cn.ggb.educenter.entity.UcenterMember;
import cn.ggb.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }


    //注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取登录信息
    @GetMapping("getMemberIndo")
    public R getMemberIndo(HttpServletRequest request){
        //调用Jwt工具类里面的方法，根据request对象获取头信息，并返回id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id查询数据库数据
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);

    }

    //跟据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberVo getUserInfo(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        UcenterMemberVo ucenterMemberVo = new UcenterMemberVo();
        BeanUtils.copyProperties(member,ucenterMemberVo);
        return ucenterMemberVo;

    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public int countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return count;
    }

}

